package com.qq.open.common;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Properties;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.log4j.Logger;

/**
 * QQ互联工具类
 *   获取各个接口的URL等工具类
 * 
 * @author HaoLiang
 * @version 0.1.1
 */
public class OpenQqUtils {

	/** 日志 */
	private static Logger log = Logger.getLogger(OpenQqUtils.class);
	
	/** 资源属性 */
	private static Properties properties;
	
	static {
		properties = new Properties();
		try {
			// 读取配置文件
			properties.load(OpenQqUtils.class.getClassLoader().getResourceAsStream(OpenQqConstants.QQ_CONFIG_NAME));
		} catch (IOException e) {
			e.printStackTrace();
			log.error("读取配置文件出错，请确认kaifangfuwu.properties文件已经放到src目录下。");
		}
	}
	
	
	
	/**
	 * 获取配置信息
	 * 
	 * @param configKey 
	 * @return 
	 */
	public String getConfigValue(String configKey) {
		String configValue = null;
		configValue = properties.getProperty(configKey);
		if (null == configValue || "".equals(configValue)) {
			log.info("没有获取指定key的值，请确认资源文件中是否存在【" + configKey + "】");
		}
		return configValue;
	}
	
	
	/**
	 * 链接QQ服务接口
	 * 
	 * @param interfaceUrl
	 *            接口URL
	 * 
	 * @return 接口返回的数据
	 * @throws IOException
	 */
	public String doGet(String interfaceUrl) throws IOException {
		// 打印日志
		log.info("doGet:" + interfaceUrl);
		
		String interfaceData = "";
		
		// 获取默认的HttpClient
		HttpClient httpclient = new DefaultHttpClient();
		
		// 获取Get连接
		HttpGet httpGet = new HttpGet(interfaceUrl);
		
		// 请求post连接
		HttpResponse response = httpclient.execute(httpGet);
		
		// 打印日志
		log.info("doGet请求状态Code:" + response.getStatusLine().getStatusCode());
		
		// 是否请求正常
		if (200 == response.getStatusLine().getStatusCode()) {

			// 获取链接返回的数据
			HttpEntity resEntity = response.getEntity();
			
			// 获取返回的数据流
			BufferedReader input = new BufferedReader(new InputStreamReader(
					resEntity.getContent(), "UTF-8"));
			String tempStr = "";

			// 获取返回的内容
			while ((tempStr = input.readLine()) != null) {
				interfaceData += tempStr.replace("\t", "");
			}
		}
		// 关闭连接
		httpGet.abort();
		
		// 打印日志
		log.info("doGet返回的json数据：" + interfaceData);
		
		return interfaceData;
	}
	

	/**
	 * 发送Post请求
	 * 
	 * @param url 请求地址
	 * @param reqEntity 请求参数
	 * @return 接口返回的数据
	 * @throws IOException
	 */
	public String doPost(String url,MultipartEntity reqEntity) throws IOException {
		// 打印日志
		log.info("doPost:" + url);
		
		// 接口返回的json数据
		String interfaceData = "";

		// 获取默认的HttpClient
		HttpClient httpclient = new DefaultHttpClient();

		// 获取post连接
		HttpPost httpPost = new HttpPost(url);

		// 设置连接参数
		httpPost.setEntity(reqEntity);

		// 请求post连接
		HttpResponse response = httpclient.execute(httpPost);
		
		// 打印日志
		log.info("doPost请求状态Code:" + response.getStatusLine().getStatusCode());

		// 是否请求正常
		if (200 == response.getStatusLine().getStatusCode()) {

			// 获取链接返回的数据
			HttpEntity resEntity = response.getEntity();
			
			// 获取返回的数据流
			BufferedReader input = new BufferedReader(new InputStreamReader(
					resEntity.getContent(), "UTF-8"));
			String tempStr = "";

			// 获取返回的内容
			while ((tempStr = input.readLine()) != null) {
				interfaceData += tempStr.replace("\t", "");
			}
		}
		// 关闭连接
		httpPost.abort();
		
		// 打印日志
		log.info("doPost返回的json数据：" + interfaceData);
		
		return interfaceData;
	}
	
	/**
	 * 判断字符串是否为null或""
	 * 
	 * @param pStr 字符串
	 * @return false 为null 或 ""  true 非空
	 */
	public boolean isNotNull(String pStr) {
		return null != pStr && !"".equals(pStr);
	}
	
	
	/**
	 * 把时间戳转换成指定的格式
	 * 
	 * @return 格式后的日期字符串
	 */
	public String timeStampToDate(String timeStamp) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); 
		// php的时间戳需要补三个0才可以正常转换
		return df.format(Long.valueOf(timeStamp + "000"));      
	}
}
